#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    mainWindowInit();
    connectInit();
}

void MainWindow::mainWindowInit()
{
    //this->setWindowFlags(Qt::FramelessWindowHint); // 设置无边框
    ui->centralWidget->installEventFilter(this);   // 安装事件过滤器

    eInput = new easyInput(this);      //输入法实例化
    eInput->installEventFilter(this);  //安装事件过滤器
    inputFlag = true;
    ui->centralWidget->setFocus();   // 界面初始化时，先把焦点在menu处，否则输入法会先出现
}

void MainWindow::connectInit()
{
    connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(exitWindow()));
}

bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
    if(watched == ui->centralWidget){
        static QPoint point;
        switch (event->type()) {
        case QEvent::MouseButtonPress:
            point.setY(cursor().pos().y());     // 记录按下点的y坐标
            eInput->hide();
            break;
        default:
            break;
        }
    }
    if(watched == eInput)
        switch (event->type()) {
        case QEvent::Hide:
            break;
        case QEvent::Show:
        {
            if(inputFlag == false){
                eInput->hide();
                return false;
            }           
            //ui->textEdit->moveCursor(QTextCursor::End);//自动滑动到最后一行
        }
            break;
        default:
            break;
        }
    return QWidget::eventFilter(watched,event);//将事件传递给父类
}

void MainWindow::slotHideInput()
{
    eInput->hide();
}

void MainWindow::exitWindow()
{
    this->close();
}

MainWindow::~MainWindow()
{
    delete ui;
}
